﻿using System;
using System.Configuration;
using System.Xml;
using FangDuShi_API.Common.DEncrypt;

namespace FangDuShi_API.Common
{
	/// <summary>
	/// web.config操作类
    /// Copyright (C) Maticsoft
	/// </summary>
	public sealed class ConfigHelper
	{
        /// <summary>
        /// 得到AppSettings中的配置字符串信息
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetConfigString(string key)
        {

            object objModel = ConfigurationManager.AppSettings[key];
            if (objModel != null)
            {
                return objModel.ToString();
            }
            else
            {
                return "";
            }



        }

		/// <summary>
		/// 得到AppSettings中的配置Bool信息
		/// </summary>
		/// <param name="key"></param>
		/// <returns></returns>
		public static bool GetConfigBool(string key)
		{
			bool result = false;
			string cfgVal = GetConfigString(key);
			if(null != cfgVal && string.Empty != cfgVal)
			{
				try
				{
					result = bool.Parse(cfgVal);
				}
				catch(FormatException)
				{
					// Ignore format exceptions.
				}
			}
			return result;
		}
		/// <summary>
		/// 得到AppSettings中的配置Decimal信息
		/// </summary>
		/// <param name="key"></param>
		/// <returns></returns>
		public static decimal GetConfigDecimal(string key)
		{
			decimal result = 0;
			string cfgVal = GetConfigString(key);
			if(null != cfgVal && string.Empty != cfgVal)
			{
				try
				{
					result = decimal.Parse(cfgVal);
				}
				catch(FormatException)
				{
					// Ignore format exceptions.
				}
			}

			return result;
		}
		/// <summary>
		/// 得到AppSettings中的配置int信息
		/// </summary>
		/// <param name="key"></param>
		/// <returns></returns>
		public static int GetConfigInt(string key)
		{
			int result = 0;
			string cfgVal = GetConfigString(key);
			if(null != cfgVal && string.Empty != cfgVal)
			{
				try
				{
					result = int.Parse(cfgVal);
				}
				catch(FormatException)
				{
					// Ignore format exceptions.
				}
			}

			return result;
		}

        /// <summary>
        /// 设置AppSettings中的相关字段的值
        /// <creator>zhongyl</creator>
        /// <date>2011-12-19</date>
        /// </summary>
        /// <param name="configKey"></param>
        /// <param name="configValue"></param>
        /// <param name="configFileName">config配置文件名称,默认值为"Training.exe.config"</param>
        public static void SeTconfigKeyValue(string configKey, string configValue, string configFileName = "Training.exe.config")
        {
            string strFileName = AppDomain.CurrentDomain.BaseDirectory.ToString() + configFileName;
            XmlDocument doc = new XmlDocument();
            doc.Load(strFileName);
            XmlNodeList nodes = doc.GetElementsByTagName("appSettings");
            for (int i = 0; i < nodes.Count; i++)
            {
                XmlNodeList cnodes = nodes[i].ChildNodes;
                for (int j = 0; j < cnodes.Count; j++)
                {

                    XmlAttributeCollection _xmlatt = cnodes[j].Attributes;

                    if (_xmlatt["key"].Value == configKey)
                    {
                        _xmlatt["value"].Value = configValue;
                        break;
                    }
                }
            }
            doc.Save(strFileName);
        }

        public void SaveAppSettingsConfig(string configKey, string configValue)
        {

        }

        /// <summary>
        /// 得到配置项的数据库连接字符串。
        /// </summary>
        /// <param name="configName"></param>
        /// <returns></returns>
        public static string GetConnectionString(string configName,string keyValue)
        {
            try
            {
                string connectionString = ConfigurationManager.AppSettings[configName];
                string ConStringEncrypt = ConfigurationManager.AppSettings["ConStringEncrypt"];
                if (ConStringEncrypt == "true")
                {
                    connectionString = DESEncrypt.Decrypt(connectionString, keyValue);
                }
                return connectionString;
            }
            catch
            {
                return "";
            }
        }

        /// <summary>
        /// 获取解密后的配置项的值
        /// </summary>
        /// <param name="configName"></param>
        /// <returns></returns>
        public static string GetDecrypConfigString(string configName, string keyValue)
        {
            object objModel = ConfigurationManager.AppSettings[configName];
            if (objModel != null)
            {
                try
                {

                    return DESEncrypt.Decrypt(objModel.ToString(), keyValue);
                }
                catch
                {
                    return "";
                }
            }
            else
            {
                return "";
            }
        }


	}
}
